package com.beetlestech.nfe.main.biz.com.impl;

import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbStudent;
import com.beetlestech.nfe.main.biz.com.dto.StudentInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.MstfileDTO;
import com.beetlestech.nfe.main.db.tbl.MststudentDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.tbl.OrderdetailDTO;
import com.beetlestech.nfe.main.db.tbl.StudentorderDTO;
import com.beetlestech.nfe.main.db.tbl.StudyhistoryDTO;
import com.beetlestech.nfe.main.db.tbl.UserjurisdictionDTO;
import com.beetlestech.nfe.main.db.view.VorderidfordeletestudentDTO;

public class HbStudentImpl extends AbstractDBLogic implements HbStudent, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "学员信息表";
	protected static final String MST_FILE = "文件表";
	
	public StudentInfoDTO getStudentInfo(Integer userID){
		StudentInfoDTO student = null;
		
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, MststudentDTO.COLUMN_USR_ID, StatementUtil.KEYWORD.EQUAL, userID);
	
		MststudentDTO searchStudent = new MststudentDTO();
		searchStudent.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		searchStudent = queryDAO.queryForObject(MststudentDTO.SQL_SEARCH, searchStudent);
		
		if(searchStudent != null)
		{
			student = convertToStudentInfo(searchStudent);
		} else {
			createApplicationException(M10010WS);
		}
		
		return student;
	}

	private StudentInfoDTO convertToStudentInfo(MststudentDTO dto) {
		StudentInfoDTO student = new StudentInfoDTO();
		student.setacademic(dto.getacademic());
		student.setareacity(dto.getareacity());
		student.setbirthdayday(dto.getbirthdayday());
		student.setbirthdaymonth(dto.getbirthdaymonth());
		student.setbirthdayyear(dto.getbirthdayyear());
		student.setfileid(dto.getfileid());
		student.setname(dto.getname());
		student.setsex(dto.getsex());
		student.setstudentid(dto.getusrid());
		student.setusrid(dto.getusrid());
		student.setgSchoolName(dto.getschool());
		student.setpName(dto.getprofessional());
		return student;
	}

	@Override
	public void UpdateUserBaseInfo(Integer usr_id, String username,
			Integer sex, Integer birthday_year, Integer birthday_month,
			Integer birthday_day, Integer areacity,String graduateSchoolName,
			String ProfessionalName) {

		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, MststudentDTO.COLUMN_USR_ID, StatementUtil.KEYWORD.EQUAL, usr_id);

		MststudentDTO searchStudent = new MststudentDTO();
		searchStudent.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		searchStudent = queryDAO.queryForObject(MststudentDTO.SQL_SEARCH, searchStudent);

		String sqlID = null;
		if(searchStudent != null) {
			sqlID = MststudentDTO.UPDATE;
		} else {
			sqlID = MststudentDTO.INSERT;
			searchStudent = new MststudentDTO();
		}

		searchStudent.setusrid(usr_id);
		searchStudent.setname(username);
		searchStudent.setsex(sex);
		searchStudent.setbirthdayyear(birthday_year);
		searchStudent.setbirthdaymonth(birthday_month);
		searchStudent.setbirthdayday(birthday_day);
		//searchStudent.setacademic(academic);
		searchStudent.setschool(graduateSchoolName);
		searchStudent.setprofessional(ProfessionalName);
		searchStudent.setareacity(areacity);
		updateDAO.updateOne(sqlID, searchStudent, M10190WS, SH_INFO);
	}

	@Override
	public void UpdateHeadImage(Integer usr_id, String path, String forwardSlash) {

		path = path.replaceAll(forwardSlash, "/");
		// mst_file表插入
		MstfileDTO mstfileDTO = new MstfileDTO();
		mstfileDTO.setpath(path);
		mstfileDTO.settype(0);
		updateDAO.updateOne(MstfileDTO.INSERT, mstfileDTO, M10040WS, MST_FILE);

		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, MststudentDTO.COLUMN_USR_ID, StatementUtil.KEYWORD.EQUAL, usr_id);
		MststudentDTO studentDTO = new MststudentDTO();
		studentDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		studentDTO = queryDAO.queryForObject(MststudentDTO.SQL_SEARCH, studentDTO);

		studentDTO.setfileid(mstfileDTO.getfileid());
		updateDAO.updateOne(MststudentDTO.UPDATE, studentDTO, M10190WS, SH_INFO);
	}

	@Override
	public void deleteStudent(String actionType, String studentSql, Integer usr_id) {
		if(actionType.equals("single")){
			MstusrDTO mstusrDTO =new MstusrDTO();
			if(usr_id==null){
				return ;
			}		
			VorderidfordeletestudentDTO vorderidfordeletestudentDTO=new VorderidfordeletestudentDTO();
			vorderidfordeletestudentDTO.setstudentid(usr_id);
			List<VorderidfordeletestudentDTO> array= queryDAO.queryForObjectList(VorderidfordeletestudentDTO.EQUAL_SEARCH, vorderidfordeletestudentDTO);
			for (VorderidfordeletestudentDTO item : array) {
				OrderdetailDTO orderdetailDTO=new OrderdetailDTO();
				orderdetailDTO.setorderid(item.getorderid());
				updateDAO.update(SQLID.ORDER_DETAIL_DELETE_FOR_STUDENT, orderdetailDTO);
				StudyhistoryDTO studyhistoryDTO=new StudyhistoryDTO();
				studyhistoryDTO.setorderid(item.getorderid());
				updateDAO.update(SQLID.STUDYHISTORY_DELETE_FOR_STUDENT, studyhistoryDTO);
				StudentorderDTO studentorderDTO=new StudentorderDTO();
				studentorderDTO.setorderid(item.getorderid());
				updateDAO.update(StudentorderDTO.DELETE, studentorderDTO);
			} 
			mstusrDTO.setusrid(usr_id);
			updateDAO.update(MstusrDTO.DELETE, mstusrDTO);
			MststudentDTO mststudentDTO =new MststudentDTO();
			mststudentDTO.setusrid(usr_id);
			updateDAO.update(MststudentDTO.DELETE, mststudentDTO);
		}else if(actionType.equals("many")){
			String[] dbSqlArray=studentSql.split(",");
			for (int i = 0; i < dbSqlArray.length; i++) {
				if(StringUtil.isEmpty(dbSqlArray[i])){
					break;
				}
				VorderidfordeletestudentDTO vorderidfordeletestudentDTO=new VorderidfordeletestudentDTO();
				vorderidfordeletestudentDTO.setstudentid(Integer.parseInt(dbSqlArray[i]));
				
				List<VorderidfordeletestudentDTO> array= queryDAO.queryForObjectList(VorderidfordeletestudentDTO.EQUAL_SEARCH, vorderidfordeletestudentDTO);
				for (VorderidfordeletestudentDTO item : array) {
					OrderdetailDTO orderdetailDTO=new OrderdetailDTO();
					orderdetailDTO.setorderid(item.getorderid());
					updateDAO.update(SQLID.ORDER_DETAIL_DELETE_FOR_STUDENT, orderdetailDTO);
					StudyhistoryDTO studyhistoryDTO=new StudyhistoryDTO();
					studyhistoryDTO.setorderid(item.getorderid());
					updateDAO.update(SQLID.STUDYHISTORY_DELETE_FOR_STUDENT, studyhistoryDTO);
					StudentorderDTO studentorderDTO=new StudentorderDTO();
					studentorderDTO.setorderid(item.getorderid());
					updateDAO.update(StudentorderDTO.DELETE, studentorderDTO);
				} 
				MstusrDTO mstusrDTO =new MstusrDTO();
				mstusrDTO.setusrid(Integer.parseInt(dbSqlArray[i]));
				updateDAO.update(MstusrDTO.DELETE, mstusrDTO);
				MststudentDTO mststudentDTO =new MststudentDTO();
				mststudentDTO.setusrid(Integer.parseInt(dbSqlArray[i]));
				updateDAO.update(MststudentDTO.DELETE, mststudentDTO);
			}
		}
		
	}
	
}
